import { Body, Controller, Headers, Post, UseGuards } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

import { ApiResult } from '~/common/decorators/api-result.decorator';
import { Ip } from '~/common/decorators/http.decorator';

import { UserService } from '../user/user.service';

import { AuthService } from './auth.service';
import { Public } from './decorators/public.decorator';
import { LoginDto, RegisterDto, SimpleLoginDto } from './dto/auth.dto';
import { LocalGuard } from './guards/local.guard';
import { LoginToken } from './models/auth.model';
import { CaptchaService } from './services/captcha.service';

@ApiTags('Auth - 认证模块')
@UseGuards(LocalGuard)
@Public()
@Controller('auth')
export class AuthController {
  constructor(private authService: AuthService, private userService: UserService, private captchaService: CaptchaService) {}

  @Post('login')
  @ApiOperation({ summary: '登录' })
  @ApiResult({ type: LoginToken })
  async login(@Body() dto: LoginDto, @Ip() ip: string, @Headers('user-agent') ua: string): Promise<LoginToken> {
    await this.captchaService.checkImgCaptcha(dto.captchaId, dto.verifyCode);
    const token = await this.authService.login(dto.username, dto.password, ip, ua);
    return { token };
  }

  @Post('simple-login')
  @ApiOperation({ summary: '简单登录，只需要账号和密码' })
  @ApiResult({ type: LoginToken })
  async simpleLogin(@Body() dto: SimpleLoginDto): Promise<LoginToken> {
    const token = await this.authService.simpleLogin(dto.username, dto.password);
    return { token };
  }

  @Post('register')
  @ApiOperation({ summary: '注册' })
  async register(@Body() dto: RegisterDto): Promise<void> {
    await this.userService.register(dto);
  }
}
